package org.example.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.model.PageResult;
import org.example.request.LockProductRequest;
import org.example.service.ProductService;
import org.example.util.JsonData;
import org.example.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LinN
 * @since 2022-10-11
 */
@Api(tags = "商品模块")
@RestController
@RequestMapping("/api/product/v1")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("分页查询商品列表")
    @GetMapping("page")
    public JsonData pageProductList(@ApiParam(value = "当前页码", required = true) @RequestParam(defaultValue = "1") int page,
                                    @ApiParam(value = "每页显示多少条", required = true) @RequestParam(defaultValue = "10") int size) {
        PageResult pageResult = productService.page(page, size);
        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("根据id查询商品")
    @GetMapping("/detail/{product_id}")
    public JsonData detail(@ApiParam(value = "商品id", required = true)
                           @PathVariable("product_id") Long productId) {
        ProductVO productVO = productService.findById(productId);
        return JsonData.buildSuccess(productVO);
    }

    @ApiOperation("锁定商品库存")
    @PostMapping("lock_products")
    public JsonData lockProducts(@ApiParam("商品库存锁定请求")
                                 @RequestBody LockProductRequest lockProductRequest) {
        JsonData jsonData = productService.lockProductStock(lockProductRequest);
        return jsonData;
    }


}

